package org.mspring.mlog.web.module.admin;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.Twitter;
import org.mspring.mlog.service.TwitterService;
import org.mspring.mlog.support.log.Log;
import org.mspring.mlog.web.query.TwitterQueryCriterion;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.Sort;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Widget
@RequestMapping(value = "/admin/twitter")
public class Admin_TwitterWidget {

    private TwitterService twitterService;

    @Autowired
    public void setTwitterService(TwitterService twitterService) {
        this.twitterService = twitterService;
    }

    @RequestMapping("/list")
    @Log
    public String listTwitter(@ModelAttribute Page<Twitter> twitterPage, @ModelAttribute Twitter twitter, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {

        if (twitterPage == null) {
            twitterPage = new Page<Twitter>();
        }
        if (twitterPage.getSort() == null) {
            twitterPage.setSort(new Sort("id", Sort.DESC));
        }
        if (twitter == null) {
            twitter = new Twitter();
        }

        twitterPage = twitterService.listPage(new TwitterQueryCriterion(queryParams), twitterPage);
        model.addAttribute("twitterPage", twitterPage);
        return "/admin/twitter/listTwitter";
    }

    @RequestMapping("/delete")
    @Log
    public String deleteTwitter(@RequestParam(required = false) Long[] id, @ModelAttribute Page<Twitter> twitterPage, @RequestParam Map queryParams, @ModelAttribute Twitter twitter, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id != null && id.length > 0) {
            twitterService.remove(id);
        }
        return listTwitter(twitterPage, twitter, queryParams, request, response, model);
    }

}
